package com.st.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.st.mall.product.dao.BrandDao;
import com.st.mall.product.dao.CategoryDao;
import com.st.mall.product.entity.BrandEntity;
import com.st.mall.product.entity.CategoryEntity;
import com.st.mall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.common.utils.PageUtils;
import com.st.common.utils.Query;

import com.st.mall.product.dao.CategoryBrandRelationDao;
import com.st.mall.product.entity.CategoryBrandRelationEntity;
import com.st.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private BrandDao brandDao;

    @Override

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationVo> getCateLogList(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>().lambda().
                eq(CategoryBrandRelationEntity::getBrandId, brandId);
        //用流式写法，stream.map返回 List<CategoryBrandRelationVo> 类型
        List<CategoryBrandRelationVo> list=
        baseMapper.selectList(wrapper).stream().map(entity->{
            CategoryBrandRelationVo cv=new CategoryBrandRelationVo();
            if(entity!=null&&entity.getCatelogId()!=null){
                CategoryEntity categoryEntity = categoryDao.selectById(entity.getCatelogId());
                cv.setCatelogId(categoryEntity.getCatId());
                cv.setCatelogName(categoryEntity.getName());
            }
            return cv;
        }).collect(Collectors.toList());
        return list ;
    }

    @Override
    public void saveCas(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //1、查询详细名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public List<CategoryBrandRelationEntity> getRelationList(Long catId) {
        List<CategoryBrandRelationEntity> list = baseMapper.selectList(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));
        return list;
    }

}